# !/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import datetime
import redis

if __name__ == '__main__':
    # 链接 redis
    redis_client = redis.Redis(host='localhost', port=6379)
    # 加载数据开始
    weibo_posts_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\Posts.json"
    weibo_comments_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\Comments.json"
    with open(weibo_posts_path, encoding="utf-8") as f:
        post_json_str = f.read()

    with open(weibo_comments_path, encoding="utf-8") as f:
        comment_json_str = f.read()

    encoding = "utf-8"
    file_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\11.txt"
    # 加载数据结束
    posts_json = json.loads(post_json_str)
    comments_json = json.loads(comment_json_str)
    posts_count = 0
    content = ""
    for post in posts_json:
        posts_mblog_id = post['mblogid']
        posts_content = "p: " + post['content'] + "\n"
        content = content + posts_content
        # with open(file_path, mode='a', encoding=encoding) as f:
        #     f.write(posts_content)
        posts_comments_count = post['comments_count']
        if posts_comments_count > 0:
            results = [item for item in comments_json if item["root_post_mblogid"] == posts_mblog_id]
            for result_item in results:
                comments_content = "    +comment:" + result_item['content'] + "\n"
                # with open(file_path, mode='a', encoding=encoding) as f:
                #     f.write(comments_content)
                content = content + comments_content

        content = content + "\n+++++++++++++++++++\n"
        posts_count = posts_count + 1
        if posts_count % 1000 == 0:
            with open(file_path, mode='a', encoding=encoding) as f:
                f.write(content)
            content = ""
        print(posts_count)
